#include "uart.h"
#include "esp_log.h"
#include <string.h>

static const char *TAG = "UART";

Uart::Uart(uart_port_t uart_num, gpio_num_t tx_pin, gpio_num_t rx_pin, int baud_rate)
    : uart_num_(uart_num), tx_pin_(tx_pin), rx_pin_(rx_pin), baud_rate_(baud_rate), initialized_(false) {
    ESP_LOGI(TAG, "UART%d创建，波特率: %d", uart_num, baud_rate);
}

Uart::~Uart() {
    if (initialized_) {
        uart_driver_delete(uart_num_);
    }
    
    ESP_LOGI(TAG, "UART%d销毁完成", uart_num_);
}

esp_err_t Uart::begin() {
    if (initialized_) {
        ESP_LOGW(TAG, "UART%d已初始化", uart_num_);
        return ESP_OK;
    }

    uart_config_t uart_config = {
        .baud_rate = baud_rate_,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };

    esp_err_t err = uart_driver_install(uart_num_, 1024, 0, 0, NULL, 0);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "UART%d驱动安装失败: %s", uart_num_, esp_err_to_name(err));
        return err;
    }

    err = uart_param_config(uart_num_, &uart_config);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "UART%d参数配置失败: %s", uart_num_, esp_err_to_name(err));
        uart_driver_delete(uart_num_); // Clean up installed driver
        return err;
    }

    err = uart_set_pin(uart_num_, tx_pin_, rx_pin_, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "UART%d引脚设置失败: %s", uart_num_, esp_err_to_name(err));
        uart_driver_delete(uart_num_); // Clean up installed driver
        return err;
    }

    initialized_ = true;
    ESP_LOGI(TAG, "UART%d初始化成功", uart_num_);
    return ESP_OK;
}

int Uart::read(uint8_t* data, uint32_t len, uint32_t timeout_ms) {
    if (!initialized_) {
        ESP_LOGE(TAG, "UART%d未初始化", uart_num_);
        return -1;
    }
    
    int result = uart_read_bytes(uart_num_, data, len, timeout_ms/portTICK_PERIOD_MS);
    
    if (result < 0) {
        ESP_LOGE(TAG, "UART%d读取失败: %d", uart_num_, result);
    }
    
    return result;
}

int Uart::write(const uint8_t* data, uint32_t len) {
    if (!initialized_) {
        ESP_LOGE(TAG, "UART%d未初始化", uart_num_);
        return -1;
    }
    
    int result = uart_write_bytes(uart_num_, data, len);
    if (result < 0) {
        ESP_LOGE(TAG, "UART%d写入失败: %d", uart_num_, result);
    }
    
    return result;
}

int Uart::write(const char* str) {
    if (!str) {
        ESP_LOGE(TAG, "字符串指针为空");
        return -1;
    }
    
    size_t str_len = strlen(str);
    
    if (!initialized_) {
        ESP_LOGE(TAG, "UART%d未初始化", uart_num_);
        return -1;
    }
    
    int result = uart_write_bytes(uart_num_, str, str_len);
    if (result < 0) {
        ESP_LOGE(TAG, "UART%d字符串写入失败: %d", uart_num_, result);
    }
    
    return result;
}

int Uart::write(const std::string& str) {
    if (!initialized_) {
        ESP_LOGE(TAG, "UART%d未初始化", uart_num_);
        return -1;
    }
    
    int result = uart_write_bytes(uart_num_, str.c_str(), str.length());
    if (result < 0) {
        ESP_LOGE(TAG, "UART%d字符串写入失败: %d", uart_num_, result);
    }
    
    return result;
}

void Uart::flush() {
    if (!initialized_) {
        ESP_LOGE(TAG, "UART%d未初始化", uart_num_);
        return;
    }
    
    esp_err_t err = uart_flush(uart_num_);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "UART%d缓冲区刷新失败: %s", uart_num_, esp_err_to_name(err));
    }
}